﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TL_Cloud.Common;

namespace TL_Cloud.Service
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var redisConfig = new RedisConfig();
            Configuration.Bind("RedisSetting", redisConfig);

            //设置允许跨域
            services.AddCors(options => options.AddPolicy("CorsPolicy",
            builder =>
            {
                builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin()
                       .AllowCredentials();
            }));
            services.AddSignalR(configure =>
            {
                configure.EnableDetailedErrors = true;
                configure.KeepAliveInterval = new TimeSpan(0, 0, 20);

            }).AddMessagePackProtocol().AddRedis(opts =>
            {
                opts.Configuration.EndPoints.Add(redisConfig.Host, redisConfig.Port);
                opts.Configuration.Password = redisConfig.Pwd;
                opts.Configuration.DefaultDatabase = 1;

            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCors("CorsPolicy");
            app.UseSignalR(route =>
            {
                route.MapHub<ChatHub>("/chathub");
            });

        }
    }
}
